using System.Collections.Generic;
using System.Xml;
using mock_ehr_lib.Data.LookupServices;

namespace mock_ehr_lib.Model.Summary
{
    public class SocialHistoryItem
    {
        public int PatientID { get; set; }
        public string Description { get; set; }
        public string Value { get; set; }
        public static IEnumerable<SocialHistoryItem> FromCCD(XmlDocument ccdXml, XmlNamespaceManager namespaceManager)
        {
            var socialHistoryItems = new List<SocialHistoryItem>();
            const string socialHistoryXPath = "//ns:observation[ns:templateId[@root='2.16.840.1.113883.10.20.1.33']]";
            var socialHistoryNodes = ccdXml.SelectNodes(socialHistoryXPath, namespaceManager);
            if (socialHistoryNodes != null)
            {
                var socialNodeEnumerator = socialHistoryNodes.GetEnumerator();
                if (socialNodeEnumerator != null)
                {
                    while (socialNodeEnumerator.MoveNext())
                    {
                        var socialHistoryItem = new SocialHistoryItem();
                        string description = null;
                        string value = null;
                        var current = socialNodeEnumerator.Current as XmlNode;
                        var codeNode = current.SelectSingleNode("./ns:code", namespaceManager);
                        if (codeNode != null)
                        {
                            var codeSystem = codeNode.Attributes["codeSystem"] == null ? null : codeNode.Attributes["codeSystem"].Value;
                            var code = codeNode.Attributes["code"] == null ? null : codeNode.Attributes["code"].Value;
                            string desc = null;
                            if (string.IsNullOrWhiteSpace(codeSystem) || string.IsNullOrWhiteSpace(code))
                            {
                                var originalTextNode = codeNode.SelectSingleNode("./ns:originalText", namespaceManager);
                                if (originalTextNode != null)
                                {
                                    desc = originalTextNode.InnerText;
                                }
                            }
                            else
                            {
                                var lookupService = LookupServiceFactory.GetLookupServiceByCodeSystem(codeSystem);
                                desc = lookupService.GetDescriptionByCode(code);
                            }
                            socialHistoryItem.Description = desc;
                        }
                        var valueNode = current.SelectSingleNode("./ns:value", namespaceManager);
                        if (valueNode != null)
                        {
                            socialHistoryItem.Value = valueNode.InnerText;
                        }
                        socialHistoryItems.Add(socialHistoryItem);
                    }
                }
            }
            return socialHistoryItems;
        }
    }
}